﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Entities;
using IRepository;
using MvcUI.Models;

namespace MvcUI.Controllers
{
    public class MRoleController : BaseController
    {
        private IRoleRepository repository;
        public MRoleController(IRoleRepository roleRepository)
        {
            this.repository = roleRepository;
        }

        [HttpGet]
        public ActionResult List(int index = 1)
        {
            ListRoleViewModel vm = new ListRoleViewModel
            {
                Roles =((IEnumerable<Entities.Role>)repository.GetList("1=1"))
                    .OrderBy(p=>p.Name)
                    .Skip((index - 1) * pageSize)
                    .Take(pageSize),
                PagingInfo = new PagingInfo
                {
                    TotalItems = repository.GetRecordCount("1=1"),
                    CurrentPage = index,
                    ItemsPerPage = pageSize,
                    OrderField = "Name",
                    OrderDirection = "desc",
                }

            };
            return View(vm);
        }

        [HttpPost]
        public ActionResult List(FormCollection formCollection)
        {
            int pageIndex = Request["pageNum"] == null ? 1 : Convert.ToInt32(Request["pageNum"]);
            pageSize = Request["numPerPage"] == null ? pageSize : Convert.ToInt32(Request["numPerPage"]);
            string orderField = Request["orderField"] == null ? "Name" : Request["orderField"].ToString();
            string orderDirection = Request["orderDirection"] == null ? "asc" : Request["orderDirection"].ToString();
            string keywords = Request["keywords"] == null ? "" : Request["keywords"].ToString();
            string strWhere = String.IsNullOrEmpty(keywords) ? "1=1" : "Name='" + keywords + "'";

            ListRoleViewModel vm = new ListRoleViewModel
            {
                Roles = ((IEnumerable<Entities.Role>)repository
                    .GetListByPage(strWhere, orderField + " " + orderDirection, (pageIndex - 1) * pageSize + 1, pageIndex * pageSize)),
               
                KeyWord = keywords,
                PagingInfo = new PagingInfo
                {
                    TotalItems = repository.GetRecordCount(strWhere),
                    CurrentPage = pageIndex,
                    ItemsPerPage = pageSize,
                    OrderField = orderField,
                    OrderDirection = orderDirection,
                }

            };
          
            return View(vm);
        }

        [HttpGet]
        public ActionResult Add()
        {
            return View();
        }
        [HttpPost]
        public string Add(Entities.Role role)
        {
            role.ID = Guid.NewGuid().ToString();
            role.Creator = this.User.ID;
            role.CreateTime = DateTime.Now;
            role.Updator = this.User.ID;
            role.UpdateTime = DateTime.Now;
            role.Description = String.IsNullOrEmpty(role.Description) ? "" : role.Description;
            role.Remark = String.IsNullOrEmpty(role.Remark) ? "" : role.Remark;
            bool result = repository.Add(role);
            if (result)
            {
                return SetOperateResultNav("200", "操作成功", "", "", "", "", "");
            }
            else
            {
                return SetOperateResultNav("300", "操作失败", "", "", "", "", "");
            }
        }


        [HttpGet]
        public ActionResult Edit(string id = "-1")
        {
            Entities.Role role = (Entities.Role)repository.Get(id);
            return View(role);
        }

        [HttpPost]
        public string Edit(Entities.Role role)
        {
            role.Updator = this.User.ID;
            role.UpdateTime = DateTime.Now;
            role.Description = String.IsNullOrEmpty(role.Description) ? "" : role.Description;
            role.Remark = String.IsNullOrEmpty(role.Remark) ? "" : role.Remark;

            bool result = repository.Update(role);
            if (result)
            {
                return SetOperateResultNav("200", "操作成功", "", "", "", "", "");
            }
            else
            {
                return SetOperateResultNav("300", "操作失败", "", "", "", "", "");
            }
        }

        [HttpPost]
        public string Delete(string id = "-1")
        {
            bool result = repository.Delete(id.ToString());
            if (result)
            {
                return SetOperateResultNav("200", "操作成功", "", "", "", "", "");
            }
            else
            {
                return SetOperateResultNav("300", "操作失败", "", "", "", "", "");
            }
        }

        [HttpPost]
        public string DeleteBatch()
        {
            List<string> listID = Request.Params["ids"] == null ? null : Request.Params["ids"].Split(',').ToList();
            int result = 0;
            if (listID.Count > 0)
            {
                result = Convert.ToInt32(repository.Delete(listID));
            }

            if (result > 0)
            {
                return SetOperateResultNav("200", "操作成功", "", "", "", "", "");
            }
            else
            {
                return SetOperateResultNav("300", "操作失败", "", "", "", "", "");
            }
        }
    }
}
